/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.LockObtainFailedException;

/**
 * Using FileContentIndexer could set up the data for indexing. But the content for
 * indexing should be in one file and every line is to be indexed. When the indexing is
 * complete, get the IndexSearcher, QueryParser and DataCollector, and use them to execute query.
 * @author Liang Jie
 */
public class FileContentIndexer {
	
	/**
	 * Instance of buffered reader to read the data source file
	 */
	protected BufferedReader bufferedreader_in;
	
	/**
	 * The writer to write the index to a directory Specified by PATH
	 * @see #PATH
	 */
	protected  IndexWriter writer;
	
	/**
	 * The DataCollector is used to calculate the unique and total items that queried 
	 */
	protected DataCollector datacollector;
	
	/**
	 * Index working path
	 */
	public static final String PATH = System.getProperty("user.dir") + java.io.File.separator
	+ "Experiment" + java.io.File.separator;
	
	/**
	 * The IndexSearcher for the indexed items
	 */
	protected IndexSearcher searcher;

	/**
	 * The name of the Field which is used to store value in Document
	 */
	public static final String FIELDNAME = "WORDS";
	
	/**
	 * Constructor, initialize the object of DataFile
	 * @param path The file to be indexed
	 */
	public FileContentIndexer(String path){

		//Read the DataSource using UTF-8 encoding
		InputStreamReader read = null;
		try {
			read = new InputStreamReader (new FileInputStream(path),"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(4);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(5);
		}
		
		bufferedreader_in = new BufferedReader(read);
		
		// set up the index writer
		try {
			writer = new IndexWriter(PATH, new StandardAnalyzer(), true);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(6);
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(7);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(8);
		}  
	}
	
	/**
	 * Start to index the file content. Notice that in this project, we don't index the files in some directory.
	 * Instead, we index the contents in a file. One line is an instance of Document in Lucene.
	 */
	public void generateIndex(){
		
		//Read a line in the datafile
		String string_words = new String();
		try {
			string_words = bufferedreader_in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(9);
		}

		/*
		 * The following is about to create the instance of Document and add to Indexwriter.
		 * In every Document, we set one Field, which is the content to be queried
		 */
		while(string_words != null){
			Document doc = new Document();
			Field field_words = new Field(FIELDNAME, string_words ,Field.Store.YES, Field.Index.TOKENIZED);
			doc.add(field_words);
			
			try {
				writer.addDocument(doc);
				string_words = bufferedreader_in.readLine();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(10);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(11);
			}
			
		}
		
		try {
			writer.close();
			bufferedreader_in.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(13);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(14);
		}
		
	}
	
}
